package com.xing.dao;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import com.xing.model.ContractInfo;
import com.xing.model.DicPayment;
import com.xing.util.Util;

@Repository
public class ContractDao {

	@Resource
	private BaseDao baseDao;

	public void setBaseDao(BaseDao baseDao) {
		this.baseDao = baseDao;
	}
	
	public void addContract(ContractInfo info) {
		
		baseDao.getHibernateTemplate().save(info);
	}
	
	public void updateContract(ContractInfo info) {
		
		baseDao.getHibernateTemplate().saveOrUpdate(info);
	}
	
	public void deleteContract(List<Integer> idList) {
		
		String hql = "delete from ContractInfo where contractId in (:contractId)";
		
		List<ParamModel> paramModelList = new ArrayList<ParamModel>();
		paramModelList.add(new ParamModel("contractId", idList));
		
		baseDao.updateByHql(hql, paramModelList);
	}
	
	public void deleteContractPayment(List<Integer> idList) {
		
		String hql = "delete from ContractPayment where contractId in (:contractId)";
		
		List<ParamModel> paramModelList = new ArrayList<ParamModel>();
		paramModelList.add(new ParamModel("contractId", idList));
		
		baseDao.updateByHql(hql, paramModelList);
	}
	
	public Map<String, Object> getListAndCount(List<String> conditionList, Integer page, Integer pageRow) {
		StringBuilder fetchHql = new StringBuilder();
		fetchHql.append("select new map(c.contractId as contractId,");
		fetchHql.append("c.contractName as contractName,");
		fetchHql.append("c.contractCode as contractCode,");
		fetchHql.append("c.contractPath as contractPath," );
		fetchHql.append("c.contractContent as contractContent,");
		fetchHql.append("c.projectId as projectId,");
		fetchHql.append("p.projectName as projectName,");
		fetchHql.append("c.amount as amount," );
		fetchHql.append("c.createUserId as createUserId," );
		fetchHql.append("c.updateUserId as updateUserId,");
		fetchHql.append("e1.employeeName as createUserName,");
		fetchHql.append("e2.employeeName as updateUserName,");
		fetchHql.append("c.createTime as createTime," );
		fetchHql.append("c.updateTime as updateTime," );
		fetchHql.append("c.remark as remark");
		fetchHql.append(")");
		
		StringBuilder hql = new StringBuilder();
		hql.append(" from ContractInfo c, ProjectInfo p, EmployeeInfo e1, EmployeeInfo e2");
		hql.append(" where c.projectId = p.projectId");
		hql.append("   and c.createUserId = e1.employeeId");
		hql.append("   and c.updateUserId = e2.employeeId");
		
		if (!Util.isEmpty(conditionList)) {
			Iterator<String> ite = conditionList.iterator();
			while (ite.hasNext()) {
				hql.append(" and ");
				hql.append(ite.next());
			}
		}
		
		StringBuilder orderHql = new StringBuilder();
		orderHql.append(" order by c.updateTime desc");
		
		Map<String, Object> result = baseDao.getListAndCount(fetchHql.toString(), hql.toString(), orderHql.toString(), page, pageRow);

		return result;
		
	}
	
	public List<Map<String, Object>> getContractPaymentList(int contractId) {
		
		StringBuilder hql = new StringBuilder();
		hql.append("select new map(paymentId as paymentId,");
		hql.append("paymentRemark as paymentRemark) ");
		hql.append("from ContractPayment where contractId = ");
		hql.append(contractId);
		hql.append(" order by paymentOrder");
		
		return (List<Map<String, Object>>) baseDao.getList(hql.toString());
	}
	
	public ContractInfo getContractInfo(int id) {
		
		return (ContractInfo) baseDao.getHibernateTemplate().load(ContractInfo.class, id);
	}
	
	public List<DicPayment> getPaymentDicList() {
		
		return (List<DicPayment>) baseDao.getList("from DicPayment");
	}
}
